package com.ruoyi.project.system.device.pdf;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class LineChartExample {
    public static void main(String[] args) {
        XYSeries series = new XYSeries("数据趋势");

        series.add(1, 10);
        series.add(2, 20);
        series.add(3, 30);
        series.add(4, 15);
        series.add(5, 25);

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);

        JFreeChart chart = ChartFactory.createXYLineChart(
                "折线图",
                "X轴",
                "Y轴",
                dataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false
        );
        // 设置整个图片的背景色
        chart.setBackgroundPaint(Color.PINK);
        // 设置图片有边框
        chart.setBorderVisible(true);
        // 获得图表区域对象
        XYPlot xyPlot = (XYPlot) chart.getPlot();
        // 设置报表区域的背景色
        xyPlot.setBackgroundPaint(Color.lightGray);
        // 设置横 纵坐标网格颜色
        xyPlot.setDomainGridlinePaint(Color.GREEN);
        xyPlot.setRangeGridlinePaint(Color.GREEN);
        // 设置横、纵坐标交叉线是否显示
        xyPlot.setDomainCrosshairVisible(true);
        xyPlot.setRangeCrosshairVisible(true);
        // 获得数据点（X,Y）的render，负责描绘数据点
        XYItemRenderer xyItemRenderer = xyPlot.getRenderer();
        if (xyItemRenderer instanceof XYLineAndShapeRenderer) {
            XYLineAndShapeRenderer xyLineAndShapeRenderer = (XYLineAndShapeRenderer) xyItemRenderer;
            xyLineAndShapeRenderer.setDefaultShapesVisible(true);
            xyLineAndShapeRenderer.setDefaultShapesFilled(true);
            xyLineAndShapeRenderer.setSeriesFillPaint(0, Color.RED); // 数据点填充为蓝色
            xyLineAndShapeRenderer.setUseFillPaint(true);// 将设置好的属性应用到render上
        }
        // 配置以下内容方可解决乱码问题
        // 配置字体
        Font xfont = new Font("宋体", Font.PLAIN, 12);  // X轴
        Font yfont = new Font("宋体", Font.PLAIN, 12);  // Y轴
        Font kfont = new Font("宋体", Font.PLAIN, 12);  // 底部
        Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题
        // 图片标题
        chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
        // 底部
        chart.getLegend().setItemFont(kfont);
        // X 轴
        ValueAxis domainAxis = xyPlot.getDomainAxis();
        domainAxis.setLabelFont(xfont);// 轴标题
        domainAxis.setTickLabelFont(xfont);// 轴数值
        domainAxis.setTickLabelPaint(Color.BLUE); // 字体颜色
        // Y 轴
        ValueAxis rangeAxis = xyPlot.getRangeAxis();
        rangeAxis.setLabelFont(yfont);
        rangeAxis.setLabelPaint(Color.BLUE); // 字体颜色
        rangeAxis.setTickLabelFont(yfont);

        int width = 800;
        int height = 600;

        File file = new File("D:\\exportPdf\\line_chart.png");
        try {
            ChartUtils.saveChartAsPNG(file, chart, width, height);
            System.out.println("折线图已保存为图片：line_chart.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}